package android.slc.commonlibrary.util;

/**
 * @author slc
 * @date 2023/9/2
 **/
public class ZoomUtils {

    /**
     * 保持比例缩放
     *
     * @param srcWidth
     * @param srcHeight
     * @param targetWidth
     * @param targetHeight
     * @return
     */
    public static RectF preserveRatioZoom(double srcWidth, double srcHeight, double targetWidth, double targetHeight) {
        double srcRatio = srcWidth / srcHeight;
        double targetRatio = targetWidth / targetHeight;
        if (targetRatio > srcRatio) {
            //目标宽高比大于原始图片宽高比
            double tempTargetWidth = targetHeight / srcHeight * srcWidth;
            return new RectF(tempTargetWidth, targetHeight);
        }
        if (targetRatio < srcRatio) {
            //目标宽高比小于原始图片宽高比
            double tempTargetHeight = targetWidth / srcWidth * srcHeight;
            return new RectF(targetWidth, tempTargetHeight);
        }
        return new RectF(targetWidth, targetHeight);
    }

    /**
     * 居中裁剪至指定比例
     *
     * @param width
     * @param height
     * @param targetRatio
     * @return
     */
    public static RectF clipByTargetRatio(int width, int height, float targetRatio,ClipGravity clipGravity) {
        RectF rectF = new RectF();
        float srcRatio = (float) width / height;
        if (srcRatio > targetRatio) {
            float targetWidth = targetRatio / srcRatio * width;
            switch (clipGravity){
                case CENTER:
                    rectF.setLeft((width - targetWidth) / 2);
                    rectF.setTop(0);
                    break;
                case START:
                    rectF.setLeft(0);
                    rectF.setTop(0);
                    break;
                case END:
                    rectF.setLeft(width - targetWidth);
                    rectF.setTop(0);
                    break;
            }
            rectF.setWidth(targetWidth);
            rectF.setHeight(height);
        } else if (srcRatio < targetRatio) {
            float targetHeight = srcRatio / targetRatio * height;
            switch (clipGravity){
                case CENTER:
                    rectF.setLeft(0);
                    rectF.setTop((height - targetHeight) / 2);
                    break;
                case START:
                    rectF.setLeft(0);
                    rectF.setTop(height - targetHeight);
                    break;
            }
            rectF.setWidth(width);
            rectF.setHeight(targetHeight);
        } else {
            rectF.left = 0;
            rectF.top = 0;
            rectF.setWidth(width);
            rectF.setHeight(height);
        }
        return rectF;
    }

    public static class RectF {
        private double left;
        private double top;
        private double width;
        private double height;

        public RectF() {
        }

        public RectF(double width, double height) {
            this(0, 0, width, height);
        }

        public RectF(double left, double top, double width, double height) {
            this.left = left;
            this.top = top;
            this.width = width;
            this.height = height;
        }

        public double getLeft() {
            return left;
        }

        public void setLeft(double left) {
            this.left = left;
        }

        public double getTop() {
            return top;
        }

        public void setTop(double top) {
            this.top = top;
        }

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }

        public double getHeight() {
            return height;
        }

        public void setHeight(double height) {
            this.height = height;
        }
    }

    public enum ClipGravity{
        CENTER,
        START,
        END
    }
}
